package com.qianfeng.smartplatform.service.impl;

//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼                  BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  
//  


import com.qianfeng.smartplatform.mapper.SysHumitureMapper;
import com.qianfeng.smartplatform.pojo.SysHumiture;
import com.qianfeng.smartplatform.service.SysHumitureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by Jackiechan on 2022/6/1 16:02
 *
 * @author Jackiechan
 * @version 1.0
 * @since 1.0
 */
@Service
@Transactional
public class SysHumitureServiceImpl  implements SysHumitureService {
    private SysHumitureMapper sysHumitureMapper;

    @Autowired
    public void setSysHumitureMapper(SysHumitureMapper sysHumitureMapper) {
        this.sysHumitureMapper = sysHumitureMapper;
    }

    @Override
    public void addsysHumiture(SysHumiture sysHumiture) {
        //校验数据
        sysHumitureMapper.addsysHumiture(sysHumiture);
    }

    @Override
    public Map findByTime(Date start, Date end) {

        if (start == null && end == null) {
            Instant now = Instant.now();//当前时间
            end = Date.from(now);
            Instant instant = now.plusSeconds(-3600);//向前倒退1小时
            start = Date.from(instant);
        }
        List<SysHumiture> humitureList = sysHumitureMapper.findByTime(start, end);

        //转换数据到三个集合
        List<Double> temperatureList = humitureList.stream().map(SysHumiture::getTemperature).collect(Collectors.toList());
        List<Double> humidityList = humitureList.stream().map(SysHumiture::getHumidity).collect(Collectors.toList());
        List<Date> timeList = humitureList.stream().map(SysHumiture::getUploadTime).collect(Collectors.toList());
        //将所有的数据保存到map中
        HashMap<Object, Object> result = new HashMap<>();
        result.put("times", timeList);
        result.put("temperatureList", temperatureList);
        result.put("humidityList", humidityList);
        return result;
    }

    @Override
    public void multiAdd(Collection<SysHumiture> sysHumitures) {
        if (sysHumitures == null||sysHumitures.size()==0) {
            return;
        }
        sysHumitureMapper.multiAdd(sysHumitures);
    }
}
